/*
 * Copyright 2008 Jens Meiss
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.googlecode.hmvc4gwt.example.hmvcblog.frontend.blog.client.blog.blogentry.comment.add;


import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.googlecode.hmvc4gwt.example.hmvcblog.frontend.blog.client.blog.blogentry.comment.add.events.in.ShowAddBlogCommentViewEvent;
import com.googlecode.hmvc4gwt.example.hmvcblog.frontend.i18n.client.I18N;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.dto.blog.entry.comment.BlogEntryCommentNewDTO;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.gwtrpc.client.blog.entry.comment.BlogEntryCommentAsyncCallback;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.gwtrpc.client.blog.entry.comment.events.out.BlogEntryCommentSuccessfullAddedEvent;
import com.googlecode.hmvc4java.event.IEvent;
import com.googlecode.hmvc4java.registry.controller.AControllerModelViewRegistry;

/**
 * 
 * @author jens.meiss
 */
public class AddCommentController extends AControllerModelViewRegistry<AddCommentModel, AddCommentView> {

	public void initView() {
		
		Grid gridAddCommentView = new Grid(3,2);
		getView().setGridAddCommentView(gridAddCommentView);
		
		Label labelAuthorName = new Label(I18N.constants.labelAuthorName());
		getView().setLabelAuthorName(labelAuthorName);
		
		TextBox textBoxAuthorName = new TextBox();
		getView().setTextBoxAuthorName(textBoxAuthorName);
		
		Label labelComment = new Label();
		getView().setLabelTextAreaComment(labelComment);
		
		TextArea textAreaComment = new TextArea();
		getView().setTextAreaComment(textAreaComment);
		
		Button buttonAddComment = new Button(I18N.constants.buttonAddBlogEntryComment());
		getView().setButtonAdd(buttonAddComment);
		
		Button buttonCancel = new Button(I18N.constants.buttonCancel());
		getView().setButtonCancel(buttonCancel);
		
		buttonAddComment.addClickListener(new ClickListener() {
			public void onClick(Widget sender) {
				addComment();
			}
		});
		
		buttonCancel.addClickListener(new ClickListener() {
			public void onClick(Widget sender) {
				cancel();
			}
		});
		
		/*
		 * build View
		 */
		gridAddCommentView.setWidget(0, 0, labelAuthorName);
		gridAddCommentView.setWidget(0, 1, textBoxAuthorName);
		gridAddCommentView.setWidget(1, 0, labelComment);
		gridAddCommentView.setWidget(1, 1, textAreaComment);
		
		HorizontalPanel horizontalPanelButtons = new HorizontalPanel();
		horizontalPanelButtons.add(buttonAddComment);
		horizontalPanelButtons.add(buttonCancel);
				
		gridAddCommentView.setWidget(2, 1, horizontalPanelButtons);
		
	}
	

	private void cancel() {
		
		getModel().getPanelParent().remove(getView().getWidget());
		getComponent().terminateComponent();
		
	}
	
	private void addComment() {
		
		BlogEntryCommentNewDTO blogEntryCommentNewDTO = new BlogEntryCommentNewDTO();
		blogEntryCommentNewDTO.setAuthor(getView().getTextBoxAuthorName().getText());
		blogEntryCommentNewDTO.setText(getView().getTextAreaComment().getText());
		blogEntryCommentNewDTO.setIdBlogEntry(getModel().getBlogEntryDTO().getId());
		
		BlogEntryCommentAsyncCallback.addBlogEntryComment(blogEntryCommentNewDTO, this);
	}

	public void init() {
		
		register(ShowAddBlogCommentViewEvent.class);
		register(BlogEntryCommentSuccessfullAddedEvent.class);
		
	}

	public void terminate() {
	}

	public void handleEvent(IEvent event) {
		
		if (event.getClass() == ShowAddBlogCommentViewEvent.class) {
			Panel panelParent = (((ShowAddBlogCommentViewEvent) event).getPanelParent());
			
			panelParent.add(getView().getWidget());
			
			getModel().setPanelParent(panelParent);
			
		} else if (event.getClass() == BlogEntryCommentSuccessfullAddedEvent.class) {
			
			
			
		}
		
	}


	public void initModel() {
	}

}
